package me.zhengjie.modules.serviceprovider.rest;

import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.serviceprovider.domain.TServiceproviderManager;
import me.zhengjie.modules.serviceprovider.domain.TServiceproviderPerson;
import me.zhengjie.modules.serviceprovider.service.TServiceproviderManagerService;
import me.zhengjie.modules.serviceprovider.service.TServiceproviderPersonService;
import me.zhengjie.modules.serviceprovider.service.dto.TServiceproviderManagerQueryCriteria;
import me.zhengjie.modules.serviceprovider.service.dto.TServiceproviderPersonQueryCriteria;
import me.zhengjie.utils.EncryptUtils;
import me.zhengjie.utils.HttpResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
* @author xuyukun
* @date 2019-07-20
*/
@RestController
@RequestMapping("api")
public class TServiceproviderPersonController {

    @Autowired
    private TServiceproviderPersonService tServiceproviderPersonService;

    @Autowired
    private TServiceproviderManagerService tServiceproviderManagerService;


    @Log("查询TServiceproviderPerson")
    @GetMapping(value = "/tServiceproviderPerson")
    //@PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERPERSON_ALL','TSERVICEPROVIDERPERSON_SELECT')")
    public ResponseEntity getTServiceproviderPersons(TServiceproviderPersonQueryCriteria criteria, Pageable pageable){
        criteria.setIsDelete(0L); //已删除不返回
        return new ResponseEntity(tServiceproviderPersonService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增TServiceproviderPerson")
    @PostMapping(value = "/tServiceproviderPerson")
    //@PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERPERSON_ALL','TSERVICEPROVIDERPERSON_CREATE')")
    public ResponseEntity create(@Validated @RequestBody TServiceproviderPerson resources){
        resources.setAccount(resources.getPhone());
        resources.setIsDelete(0);
        return new ResponseEntity(tServiceproviderPersonService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改TServiceproviderPerson")
    @PutMapping(value = "/tServiceproviderPerson")
    //@PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERPERSON_ALL','TSERVICEPROVIDERPERSON_EDIT')")
    public ResponseEntity update(@Validated @RequestBody TServiceproviderPerson resources){
        resources.setIsDelete(0); //非删除
        tServiceproviderPersonService.update(resources);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("删除TServiceproviderPerson")
    @DeleteMapping(value = "/tServiceproviderPerson/{id}")
    //@PreAuthorize("hasAnyRole('ADMIN','TSERVICEPROVIDERPERSON_ALL','TSERVICEPROVIDERPERSON_DELETE')")
    public ResponseEntity delete(@PathVariable Long id) {
        try {
            tServiceproviderPersonService.delete(id);
            return new ResponseEntity(HttpStatus.OK);
        } catch (Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }

    @Log("查询TServiceproviderManager")
    @GetMapping(value = "/tServiceproviderManager")
    //@PreAuthorize("hasAnyRole('ADMIN','TServiceproviderManager_ALL','TServiceproviderManager_SELECT')")
    public ResponseEntity getTServiceproviderManagers(TServiceproviderManagerQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(tServiceproviderManagerService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增TServiceproviderManager")
    @PostMapping(value = "/tServiceproviderManager")
    //@PreAuthorize("hasAnyRole('ADMIN','TServiceproviderManager_ALL','TServiceproviderManager_CREATE')")
    public ResponseEntity createManager(@Validated @RequestBody TServiceproviderManager resources){
        resources.setIsManager(0);
        resources.setPassword(EncryptUtils.encryptPassword("123456"));
        return new ResponseEntity(tServiceproviderManagerService.create(resources),HttpStatus.CREATED);
    }

    @Log("修改TServiceproviderManager")
    @PutMapping(value = "/tServiceproviderManager")
    //@PreAuthorize("hasAnyRole('ADMIN','TServiceproviderManager_ALL','TServiceproviderManager_EDIT')")
    public ResponseEntity updateManager(@Validated @RequestBody TServiceproviderManager resources){
        tServiceproviderManagerService.update(resources);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("删除TServiceproviderManager")
    @DeleteMapping(value = "/tServiceproviderManager/{id}")
    //@PreAuthorize("hasAnyRole('ADMIN','TServiceproviderManager_ALL','TServiceproviderManager_DELETE')")
    public ResponseEntity deleteManager(@PathVariable Long id) {
        try {
            tServiceproviderManagerService.delete(id);
            return new ResponseEntity(HttpStatus.OK);
        } catch (Exception ex) {
            ex.printStackTrace();
            return HttpResponseUtils.passDefaultResponse(ex);
        }
    }
}